package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IDeviceDao;
import cn.edu.seu.ssny.dao.IProjectDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.*;
import cn.edu.seu.ssny.service.IDeviceService;
import cn.edu.seu.ssny.service.IDeviceTemplateService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * 设备表
 */
@Service
public class DeviceServiceImpl extends BaseServiceImpl<Device, Integer> implements IDeviceService {

    @Autowired
    private IDeviceDao deviceDao;

    @Autowired
    private IDeviceTemplateService deviceTemplateService;

    @Override
    public IBaseDao<Device, Integer> getBaseDao() {
        return this.deviceDao;
    }

    @Override
    public List<Device> findByProject(Project project) {
        return deviceDao.findByProject(project);
    }

    @Override
    public Optional<Device> findByNameAndProject(String name, Project project) {
        return deviceDao.findByNameAndProject(name, project);
    }

    @Override
    public List<Device> findByEnterprise(Enterprise enterprise) {
        return deviceDao.findByEnterprise(enterprise);
    }

    @Override
    public Device findByName(String name) {
        return deviceDao.findByName(name);
    }

    @Override
    public Device findByProjectAndGatewayAndTag(Project project, Gateway gateway, String tag) {
        return deviceDao.findByProjectAndGatewayAndTag(project, gateway, tag);
    }

    @Override
    public List<Device> findAlarmDevices(Project project) {
        DeviceTemplate alarmTemplate = deviceTemplateService.findAlarm();
        return deviceDao.findByProjectAndTemplateOrderByName(project, alarmTemplate);
    }

    @Override
    public Optional<Device> findByEquipmentAndDescriptionLike(Equipment equipment, String name) {
        return deviceDao.findByEquipmentAndDescriptionLike(equipment, name);
    }

    @Override
    public void saveOrUpdate(Device device, Enterprise enterprise, Project project, Gateway gateway, DeviceTemplate template) {
        if (device.getId() != null) {
            Device dbDevice = find(device.getId());
            dbDevice.setName(device.getName());
            dbDevice.setTag(device.getTag());
            dbDevice.setPeriod(device.getPeriod());
            if (device.getDescription() != null) {
                dbDevice.setDescription(device.getDescription().trim());
            }
            dbDevice.setNumber(device.getNumber());
            dbDevice.setTemplate(template);
            dbDevice.setGateway(gateway);
            dbDevice.setProject(project);
            dbDevice.setStatus(device.getStatus());
            dbDevice.setDisplay(device.getDisplay());
            dbDevice.setCommunicationStation(device.getCommunicationStation());
            update(dbDevice);
        } else {
            device.setTemplate(template);
            device.setGateway(gateway);
            device.setProject(project);
            device.setEnterprise(enterprise);
            save(device);
        }
    }

    @Override
    public void saveCoordinate(Device device) {
        Device dbDevice = find(device.getId());
        dbDevice.setCoordinateX(device.getCoordinateX());
        dbDevice.setCoordinateY(device.getCoordinateY());
        dbDevice.setDisplay(device.getDisplay());
        update(dbDevice);
    }

    @Override
    public void setCategory(Device device, DeviceCategory category) {
        Device dbDevice = find(device.getId());
        dbDevice.setCategory(category);
        update(dbDevice);
    }

    @Override
    public void deleteCategory(Device device) {
        Device dbDevice = find(device.getId());
        dbDevice.setCategory(null);
        update(dbDevice);
    }

    @Override
    public void setCategory(Device device, DeviceCategoryCommon category) {
        Device dbDevice = find(device.getId());
        dbDevice.setCategoryCommon(category);
        update(dbDevice);
    }

    @Override
    public void deleteCommonCategory(Device device) {
        Device dbDevice = find(device.getId());
        dbDevice.setCategoryCommon(null);
        update(dbDevice);
    }


    @Override
    public void setDevicesByEquipment(Equipment equipment, String[] deviceIds) {
        List<Device> devices = equipment.getDevices();
        for (Device device : devices) {
            device.setEquipment(null);
            update(device);
        }

        if (deviceIds != null) {
            for (int i = 0; i < deviceIds.length; i++) {
                if (StringUtils.isBlank(deviceIds[i]) || "0".equals(deviceIds[i])) {
                    continue;
                }
                Integer rid = Integer.parseInt(deviceIds[i]);
                Device device = find(rid);
                device.setEquipment(equipment);
                update(device);
            }
        }
    }

    @Override
    public Device findByEquipmentAndDescription(Equipment equipment, String description) {
        return deviceDao.findByEquipmentAndDescription(equipment, description);
    }

    @Override
    public List<Device> findByEquipmentInAndDescriptionLike(List<Equipment> equipments, String desc) {
        return deviceDao.findByEquipmentInAndDescriptionLike(equipments, desc);
    }
}